import { rowLocation, colLocation, mouseposition } from '../global/location';
import { selectHightlightShow } from './select';
import menuButton from './menuButton';
import luckysheetFreezen from './freezen';
import Store from '../store';
import luckysheetDropCell from './dropCell';
import { getSheetIndex } from '../methods/get';
import selection from './selection';
import {   luckysheetactiveCell} from '../utils/util';
import {  hasPartMC , isEditMode } from '../global/validate';
import editor from '../global/editor'

//设备是移动端
export default function mobileinit(){    
    // nby 移动端，长按拖动的圆点
    const box = $('.luckysheet-cs-touchhandle-btn')[1]
    
    function a(event){
        event.target.style.borderRadius='0%'
        Store.luckysheet_cell_selected_extend = true
 
    }

    function addLongtabListener(target,callback){
        let timer=0   // 初始化timer

        target.ontouchstart=(e)=>{
            timer=0   // 重置timer
            timer=setTimeout(()=>{callback(e);timer=0},380)  // 超时器能成功执行，说明是长按
        }
        target.ontouchmove=()=>{
            clearTimeout(timer)    // 如果来到这里，说明是滑动
            timer=0 
        }
        target.ontouchend=()=>{   // 到这里如果timer有值，说明此触摸时间不足380ms，是点击
        if(timer){clearTimeout(timer)}
        }
    }
    // addLongtabListener(box,a)

    //去除滚动条
    Store.cellMainSrollBarSize = 0;

    //滑动滚动表格
    let luckysheet_touchmove_status = false,
        luckysheet_touchmove_startPos = {},
        luckysheet_touchhandle_status = false,
        _scrollTimer = null;
    let pressTimer;
    $(document).on("touchstart", "#luckysheet-grid-window-1", function(event){    
  
        // nby 第二步、点击元素不在操作区域时，清空操作区域
        if(!event.target.contains($('.luckysheet-cs-touchhandle-btn')[1])){
            $('#mobileOperation').remove()
        }

        clearInterval(_scrollTimer);//clear timer
        luckysheet_touchmove_status = true;

        let touch = event.originalEvent.targetTouches[0];
        luckysheet_touchmove_startPos = {
            x: touch.pageX,
            y: touch.pageY,
            vy:0, //vy可以理解为滑动的力度
            moveType:"y",
        }
        // nby 长按才出现复制等按钮
        pressTimer = setTimeout(()=>{
            // navigator?.vibrate(100);
            if(Store.isFormApp && Store.isMobile){
                // nby 选中格子需要所有都为isDetail是才能弹出操作界面 针对lxc的数字表格
                const curSelect = Store.luckysheet_select_save
                const curSelRow = curSelect[0].row
                const curSelCol = curSelect[0].column
                let curData = Store.flowdata.slice(curSelRow[0], curSelRow[1] + 1)
                curData = curData.map(arr=>arr.slice(curSelCol[0], curSelCol[1] + 1))
                const curDataLen = curData.length
                let isShow = true
                for(let i= 0;i < curDataLen;i++){
                    for(let j = 0; j < curData[i].length; j++ ){
                        let curCell = curData[i][j]
                        if(curCell?.isDetail != true){
                            // 不是明细行
                            isShow = false
                        }
                    }
                }
                ShowMobileOperation(event)
                if(isShow){
                    // 明细行，不需要合并
                    $('#mergeCellMobile').hide()
                }else{
                    // 不是明细行，显示复制文本、全复制、粘贴、删除内容
                    $('#mergeCellMobile').hide()
                    $('#upInsert').hide()
                    $('#downInsert').hide()
                    $('#delRow').hide()
                }
            }else{
                ShowMobileOperation(event)
            }
        },500)
    })
    $(document).on("touchmove", "#luckysheet-grid-window-1", function(event){
        // nby 滑动时不弹出复制区域
        if(pressTimer){
            clearTimeout(pressTimer)        
            pressTimer = null
        }

        if(event.originalEvent.targetTouches.length > 1 || (event.scale && event.scale !== 1)){
            return;
        }

        let touch = event.originalEvent.targetTouches[0];

        if(luckysheet_touchmove_status){//滚动
            let slideX = touch.pageX - luckysheet_touchmove_startPos.x;
            let slideY = touch.pageY - luckysheet_touchmove_startPos.y;

            luckysheet_touchmove_startPos.x = touch.pageX;
            luckysheet_touchmove_startPos.y = touch.pageY;

            let scrollLeft = $("#luckysheet-scrollbar-x").scrollLeft();
            let scrollTop = $("#luckysheet-scrollbar-y").scrollTop();


            scrollLeft -= slideX;
            scrollTop -= slideY;


            if(scrollLeft < 0){
                scrollLeft = 0;
            }

            if(scrollTop < 0){
                scrollTop = 0;
            }
            
            $("#luckysheet-scrollbar-y").scrollTop(scrollTop);

            luckysheet_touchmove_startPos.vy_y = slideY;
            luckysheet_touchmove_startPos.scrollTop = scrollTop;

            $("#luckysheet-scrollbar-x").scrollLeft(scrollLeft);

            luckysheet_touchmove_startPos.vy_x = slideX;

            luckysheet_touchmove_startPos.scrollLeft = scrollLeft;
   

        }
        else if(luckysheet_touchhandle_status){//选区
            let mouse = mouseposition(touch.pageX, touch.pageY);
            let x = mouse[0] + $("#luckysheet-cell-main").scrollLeft();
            let y = mouse[1] + $("#luckysheet-cell-main").scrollTop();

            let row_location = rowLocation(y), 
                row = row_location[1], 
                row_pre = row_location[0], 
                row_index = row_location[2];
            let col_location = colLocation(x), 
                col = col_location[1], 
                col_pre = col_location[0], 
                col_index = col_location[2];

            let last = $.extend(true, {}, Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1]);

            let top = 0, height = 0, rowseleted = [];
            if (last.top > row_pre) {
                top = row_pre;
                height = last.top + last.height - row_pre;

                if(last.row[1] > last.row_focus){
                    last.row[1] = last.row_focus;
                }

                rowseleted = [row_index, last.row[1]];
            }
            else if (last.top == row_pre) {
                top = row_pre;
                height = last.top + last.height - row_pre;
                rowseleted = [row_index, last.row[0]];
            }
            else {
                top = last.top;
                height = row - last.top - 1;

                if(last.row[0] < last.row_focus){
                    last.row[0] = last.row_focus;
                }

                rowseleted = [last.row[0], row_index];
            }

            let left = 0, width = 0, columnseleted = [];
            if (last.left > col_pre) {
                left = col_pre;
                width = last.left + last.width - col_pre;

                if(last.column[1] > last.column_focus){
                    last.column[1] = last.column_focus;
                }

                columnseleted = [col_index, last.column[1]];
            }
            else if (last.left == col_pre) {
                left = col_pre;
                width = last.left + last.width - col_pre;
                columnseleted = [col_index, last.column[0]];
            }
            else {
                left = last.left;
                width = col - last.left - 1;

                if(last.column[0] < last.column_focus){
                    last.column[0] = last.column_focus;
                }

                columnseleted = [last.column[0], col_index];
            }

            let changeparam = menuButton.mergeMoveMain(columnseleted, rowseleted, last, top, height, left, width);
            if(changeparam != null){
                columnseleted = changeparam[0];
                rowseleted= changeparam[1];
                top = changeparam[2];
                height = changeparam[3];
                left = changeparam[4];
                width = changeparam[5];
            }

            last["row"] = rowseleted;
            last["column"] = columnseleted;

            last["left_move"] = left;
            last["width_move"] = width;
            last["top_move"] = top;
            last["height_move"] = height;

            Store.luckysheet_select_save[Store.luckysheet_select_save.length - 1] = last;

            selectHightlightShow();
            luckysheetFreezen.scrollFreezen();
        }

        event.stopPropagation();
    })
    $(document).on("touchend", function(event){
        
        if(luckysheet_touchmove_status){
            let vy_x = Math.abs(luckysheet_touchmove_startPos.vy_x), friction_x = ((vy_x >> 31) * 2 + 1) * 0.25;

            let vy_y = Math.abs(luckysheet_touchmove_startPos.vy_y), friction_y = ((vy_y >> 31) * 2 + 1) * 0.25;
            if(vy_x>0 || vy_y>0){
                _scrollTimer = setInterval(function () {//
                    vy_x -= friction_x;//力度按 惯性的大小递减
                    vy_y -= friction_y;//力度按 惯性的大小递减                 

                    if(vy_x<=0){
                        vy_x = 0;
                    }
                    if(vy_y<=0){
                        vy_y = 0;
                    }
         
                    if(luckysheet_touchmove_startPos.vy_y>0){
                        luckysheet_touchmove_startPos.scrollTop -= vy_y;
                    }
                    else{
                        luckysheet_touchmove_startPos.scrollTop += vy_y;
                    }
            
                    $("#luckysheet-scrollbar-y").scrollTop(luckysheet_touchmove_startPos.scrollTop);
            
                    if(luckysheet_touchmove_startPos.vy_x>0){
                        luckysheet_touchmove_startPos.scrollLeft -= vy_x;
                    }
                    else{
                        luckysheet_touchmove_startPos.scrollLeft += vy_x;
                    }
            
                    $("#luckysheet-scrollbar-x").scrollLeft(luckysheet_touchmove_startPos.scrollLeft);
         
                    if(vy_x<=0 && vy_y<=0){
                        clearInterval(_scrollTimer);
                    }
                }, 20); 
            }

        }
        luckysheet_touchmove_status = false;
        // luckysheet_touchmove_startPos = {};
        luckysheet_touchhandle_status = false;
        // nby 显示复制区域块
        // ShowMobileOperation(event)
        clearTimeout(pressTimer)
        pressTimer = null
    })

    //滑动选择选区
    $(document).on("touchstart", ".luckysheet-cs-touchhandle", function(event){
        luckysheet_touchhandle_status = true;
        luckysheet_touchmove_status = false;
        event.stopPropagation();
    })  

    //禁止微信下拉拖出微信背景
    // 源码：
    /*
    document.addEventListener("touchmove", function(event){
        event.preventDefault();
    }, {
        passive: false
    })
    */ 
    // END
    // nby 移动端，解决影响其他页面的滚动
    document.addEventListener("touchmove",Store.preDefault, {
        passive: false
    })

    function ShowMobileOperation(event){
        // nby 第一步、判断触摸完毕是否显示复制块
        // if(event.target.contains($('.luckysheet-cs-touchhandle-btn')[1]) && $("#mobileOperation").length==0){
        if( $("#mobileOperation").length==0){

            // nby 移动端操作区域dom
            if(Store.isMobile){//这个是Store.isMobile是在全局新加的变量，用以表示是移动端，因为luckysheet本身判断有问题，只能手动填写
                $("body").append(`
                <div id='mobileOperation' style="z-index:10000;position: absolute;width: 200px;height: auto;user-select:none;">
                    <div style="display: flex; ">
                        <div style="flex-wrap:wrap;background-color:wheat;display: flex;justify-content:space-between;align-item:center;padding: 6px;width:auto;border-radius: 10px;">
				            <div id='mobileCopyBtn' style="display: flex;cursor: pointer;margin-right: 6px;width: 80px;
                                overflow: hidden; height: 20px; line-height: 20px;padding: 4px;"><svg style="width:18px;height:18px;margin:0 2px;" t="1673399639231" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1908" width="200" height="200"><path d="M720 192h-544A80.096 80.096 0 0 0 96 272v608C96 924.128 131.904 960 176 960h544c44.128 0 80-35.872 80-80v-608C800 227.904 764.128 192 720 192z m16 688c0 8.8-7.2 16-16 16h-544a16 16 0 0 1-16-16v-608a16 16 0 0 1 16-16h544a16 16 0 0 1 16 16v608z" p-id="1909"></path><path d="M848 64h-544a32 32 0 0 0 0 64h544a16 16 0 0 1 16 16v608a32 32 0 1 0 64 0v-608C928 99.904 892.128 64 848 64z" p-id="1910"></path><path d="M608 360H288a32 32 0 0 0 0 64h320a32 32 0 1 0 0-64zM608 520H288a32 32 0 1 0 0 64h320a32 32 0 1 0 0-64zM480 678.656H288a32 32 0 1 0 0 64h192a32 32 0 1 0 0-64z" p-id="1911"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>全复制</span>
                            </div>
				            <div id='mobilePasteBtn' style="display: flex;border: none;cursor: pointer;margin-right: 6px;
                            width: 80px;overflow: hidden; height: 20px; line-height: 20px;padding: 4px;"><svg style="width:18px;height:18px;margin-right: 4px;" t="1673399835334" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2894" width="200" height="200"><path d="M901.117698 948.224208V814.080577H959.997536v134.143631h-58.879838z m0-328.959095H959.997536v133.887632h-58.879838v-133.887632z m0-198.655454H959.997536v133.887632h-58.879838v-133.887632z m0-207.359429H959.997536v134.143631h-58.879838V213.25023z m-153.599578 752.125931h120.319669V1024h-120.319669v-58.623839z m43.238281-162.303553a93.490943 93.490943 0 0 1-94.23334 92.671745H279.833006v78.079785h-58.879838v-78.079785H158.284541a93.490943 93.490943 0 0 1-94.233341-92.671745V306.689973L355.096799 0.002816H696.57426a93.490943 93.490943 0 0 1 94.233341 92.671745v26.879926H865.277796v58.623839h-74.572594v624.894282zM162.27813 306.177974h203.289041V91.906563z m557.259267-213.503413a22.630338 22.630338 0 0 0-23.039936-22.015939H436.837375v245.247325a61.260632 61.260632 0 0 1-61.439831 60.927833H135.142204v426.238828a22.476738 22.476738 0 0 0 23.039937 22.015939H696.57426a22.451138 22.451138 0 0 0 23.039937-22.015939V92.674561zM450.558937 1024h-120.319669v-58.623839h120.319669V1024z m212.479416 0h-120.319669v-58.623839h120.319669V1024z" fill="#333333" p-id="2895"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>粘贴</span>
                            </div>
                            <div id='copyShowVal' style="display: flex;cursor: pointer;margin-right: 6px;
                            width: 80px; overflow: hidden; height: 20px; line-height: 20px;padding: 4px;
                            "><svg style="width:20px;height:20px;" t="1675401312010" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1855" width="200" height="200"><path d="M725.333333 341.333333h128v512H341.333333v-128H213.333333V213.333333h512v128z m0 42.666667v341.333333H384v85.333334h426.666667V384h-85.333334zM256 256v426.666667h426.666667V256H256z" fill="#444444" p-id="1856"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>复制文本</span>
                            </div>
                            <div id='mergeCellMobile' style="display: flex;cursor: pointer;margin-right: 6px;
                            width: 80px; overflow: hidden; height: 20px; line-height: 20px;padding: 4px;
                            "><svg style="width:16px;height:20px;margin-right: 4px"  t="1678064805144" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2751" width="200" height="200"><path d="M362.375529 613.556706a29.033412 29.033412 0 0 1-20.720941-8.613647l-0.963764-1.024a33.611294 33.611294 0 0 1-0.90353-45.116235l45.296941-47.706353h-224.677647V445.620706h224.195765l-44.393412-46.742588a33.611294 33.611294 0 0 1-0.903529-45.056l0.903529-1.084236a29.274353 29.274353 0 0 1 42.405647-1.084235l120.591059 126.976-119.145412 125.289412a28.912941 28.912941 0 0 1-21.684706 9.637647m340.931765 0a29.033412 29.033412 0 0 1-20.781176-8.613647L561.995294 478.027294l119.024941-125.289412a29.274353 29.274353 0 0 1 42.405647-1.084235l0.90353 1.084235a33.310118 33.310118 0 0 1 0.963764 44.754824l-45.357176 47.706353h224.256v65.897412h-224.075294l44.333176 46.742588a33.129412 33.129412 0 0 1 0.963765 44.634353l-0.963765 1.084235v0.481882a27.587765 27.587765 0 0 1-21.202823 9.517177m-579.102118 358.821647c-41.441882-3.553882-73.246118-39.152941-73.246117-83.727059V93.364706C50.898824 46.381176 86.136471 9.216 131.011765 9.216h267.986823c44.875294 0 80.112941 36.984471 80.112941 84.208941V177.694118h-62.644705V93.424941a19.456 19.456 0 0 0-4.818824-13.191529 18.672941 18.672941 0 0 0-12.528941-5.662118H130.951529a18.010353 18.010353 0 0 0-12.468705 5.541647 17.106824 17.106824 0 0 0-4.818824 12.769883v794.563764c0 11.685647 6.264471 18.251294 17.287529 18.251294h268.047059a18.010353 18.010353 0 0 0 12.468706-5.541647 17.106824 17.106824 0 0 0 4.818824-12.649411v-84.208942h62.223058v84.148706c0 47.224471-35.237647 84.208941-80.112941 84.208941H124.084706l0.12047 0.722824z m542.479059 0.481882c-44.815059 0-80.052706-36.984471-80.052706-84.208941v-84.208941h62.644706v84.208941c0 11.625412 6.264471 18.251294 17.287529 18.251294h268.649412a19.937882 19.937882 0 0 0 17.28753-18.251294V93.906824c0-11.625412-6.264471-18.251294-17.28753-18.251295h-268.528941a19.937882 19.937882 0 0 0-17.287529 18.251295V180.705882H586.691765V93.967059c0-47.224471 35.237647-84.208941 80.112941-84.208941h267.986823c44.875294 0 80.112941 36.984471 80.112942 84.208941v794.74447c0 47.224471-35.237647 84.208941-80.112942 84.208942h-268.167529z" fill="#666666" p-id="2752"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>合并/取消</span>
                            </div>

                            <div id='upInsert' style="display: flex;cursor: pointer;margin-right: 6px;
                            width: 80px; overflow: hidden; height: 20px; line-height: 20px;padding: 4px;
                            "><svg style="width:16px;height:20px;margin-right: 4px" t="1681103659307" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2813" width="200" height="200"><path d="M493.21 94.78l-63.83 67.68 106.39 100.62H1.03v93.09h768.59zM1.11 433.92v439.43h1022.73V433.92H1.11z m929.75 93.09v80.07H94.08v-80.07h836.78zM94.08 780.26v-80.09h836.78v80.09H94.08z" fill="#231815" p-id="2814"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>上插入行</span>
                            </div>
                            <div id='downInsert' style="display: flex;cursor: pointer;margin-right: 6px;
                            width: 80px; overflow: hidden; height: 20px; line-height: 20px;padding: 4px;
                            "><svg style="width:16px;height:20px;margin-right: 4px" t="1681103684151" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2983" width="200" height="200"><path d="M493.21 931.37l-63.83-67.68 106.39-100.61H1.03v-93.09h768.59zM1.11 592.24V152.81h1022.73v439.43H1.11z m929.75-93.1v-80.07H94.08v80.07h836.78zM94.08 245.9v80.09h836.78V245.9H94.08z" fill="#231815" p-id="2984"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>下插入行</span>
                            </div>
                            <div id='delRow' style="display: flex;cursor: pointer;margin-right: 6px;
                            width: 80px; overflow: hidden; height: 20px; line-height: 20px;padding: 4px;
                            "><svg style="width:16px;height:20px;margin-right: 4px" t="1681103590785" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2640" width="200" height="200"><path d="M847.743 223.953H640.639c-3.132-68.921-60.177-124.029-129.858-124.029s-126.726 55.108-129.858 124.029H173.256c-17.673 0-32 14.327-32 32s14.327 32 32 32h674.487c17.673 0 32-14.327 32-32s-14.327-32-32-32z m-336.962-60.03c34.379 0 62.689 26.426 65.718 60.029H445.064c3.029-33.603 31.338-60.029 65.717-60.029zM767.743 351.79c-17.673 0-32 14.327-32 32v478.173H288.256V383.79c0-17.673-14.327-32-32-32s-32 14.327-32 32v510.173c0 17.673 14.327 32 32 32h511.487c17.673 0 32-14.327 32-32V383.79c0-17.673-14.327-32-32-32z" fill="" p-id="2641"></path><path d="M449.306 732.802V448.208c0-17.673-14.327-32-32-32s-32 14.327-32 32v284.593c0 17.673 14.327 32 32 32s32-14.326 32-31.999zM640.84 732.802V448.208c0-17.673-14.327-32-32-32s-32 14.327-32 32v284.593c0 17.673 14.327 32 32 32s32-14.326 32-31.999z" fill="" p-id="2642"></path></svg>
                                <span style='font-size: 14px;white-space: nowrap;'>删除行</span>
                            </div>
                            <div id='delCell' style="display: flex;cursor: pointer;margin-right: 6px;
                            width: 80px; overflow: hidden; height: 20px; line-height: 20px;padding: 4px;
                            "><svg style="width:16px;height:20px;margin-right: 4px" t="1698908569863" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4967" width="200" height="200"><path d="M274.56 798.997333l19.434667-25.130666-33.792 68.565333a18.133333 18.133333 0 0 0 11.562666 25.536l59.733334 16a18.133333 18.133333 0 0 0 17.28-4.48c20.522667-19.818667 35.626667-35.989333 45.290666-48.469333l19.456-25.130667-33.813333 68.565333a18.133333 18.133333 0 0 0 11.562667 25.536l84.48 22.634667a18.133333 18.133333 0 0 0 17.28-4.48c20.522667-19.84 35.626667-35.989333 45.269333-48.469333l19.456-25.130667-33.813333 68.565333A18.133333 18.133333 0 0 0 535.530667 938.666667l72.106666 19.328a18.133333 18.133333 0 0 0 17.28-4.48c20.522667-19.84 35.626667-36.010667 45.269334-48.490667l19.456-25.130667-33.813334 68.586667a18.133333 18.133333 0 0 0 11.584 25.514667l86.421334 23.338666 3.84-0.213333c13.269333-0.704 29.056-5.034667 43.84-12.8 29.781333-15.701333 48.170667-43.2 52.181333-78.250667 2.133333-18.517333 4.778667-38.549333 8.405333-63.530666 1.642667-11.221333 2.944-20.010667 6.229334-41.834667 11.050667-73.322667 14.634667-101.034667 17.130666-133.674667l0.938667-12.373333 2.837333-2.922667 12.330667-1.344a41.813333 41.813333 0 0 0 24.810667-11.221333c10.730667-10.24 14.805333-25.386667 11.093333-42.197333l-37.546667-171.584c-3.029333-13.696-11.264-27.946667-23.146666-39.829334-11.648-11.626667-25.92-20.138667-39.893334-23.893333L723.626667 331.306667l-2.261334-3.925334L774.250667 130.133333c8.32-31.061333-11.754667-63.744-44.970667-72.64l-79.509333-21.312c-33.194667-8.896-66.922667 9.365333-75.264 40.426667l-52.842667 197.269333-3.925333 2.261334-118.101334-31.637334c-13.994667-3.754667-30.634667-3.498667-46.506666 0.746667-16.256 4.352-30.506667 12.586667-39.957334 22.933333l-118.314666 129.792c-11.605333 12.714667-15.658667 27.84-11.52 42.090667 4.16 14.229333 15.850667 25.194667 32.896 30.528l13.610666 4.266667 2.133334 3.882666-3.626667 13.802667c-21.12 79.850667-52.885333 136.917333-85.717333 150.890667-47.530667 20.202667-72.938667 49.429333-78.421334 85.034666-5.034667 32.682667 9.28 67.114667 37.589334 91.541334l22.037333 8.341333 74.666667 20.010667a42.666667 42.666667 0 0 0 41.216-11.050667c15.274667-15.274667 26.88-28.032 34.837333-38.293333z m551.381333-396.565333c14.144 3.797333 29.952 19.2 32.768 32l34.56 157.781333a10.666667 10.666667 0 0 1-13.184 12.586667L240.64 433.493333a10.666667 10.666667 0 0 1-5.12-17.493333l108.8-119.36c8.832-9.685333 30.229333-15.146667 44.373333-11.349333l141.333334 37.866666a21.333333 21.333333 0 0 0 26.133333-15.082666l58.304-217.642667a21.333333 21.333333 0 0 1 26.133333-15.082667l77.056 20.650667a21.333333 21.333333 0 0 1 15.082667 26.133333l-58.325333 217.642667a21.333333 21.333333 0 0 0 15.082666 26.112l136.448 36.565333zM315.456 701.568c-33.664 45.141333-64.597333 79.082667-92.8 101.802667l-5.909333 4.778666-2.837334 0.597334-88.106666-24.106667-2.922667-3.2c-13.034667-14.165333-19.370667-31.04-16.981333-46.592 3.285333-21.333333 22.058667-39.338667 53.205333-52.586667 31.722667-13.482667 59.818667-47.104 82.922667-99.904 10.026667-22.954667 18.88-48.725333 26.389333-76.586666l3.882667-14.4 3.904-2.261334 566.165333 151.701334 2.346667 3.306666-0.789334 12.224c-1.984 30.592-30.336 229.397333-32.128 244.906667-2.346667 20.416-11.306667 34.986667-27.605333 44.394667a73.237333 73.237333 0 0 1-21.397333 8.106666l-5.013334 0.725334-60.373333-16.170667 11.242667-20.288c8.277333-14.976 22.656-43.84 43.093333-86.613333a21.12 21.12 0 0 0-9.962667-28.16l-3.136-1.493334a21.333333 21.333333 0 0 0-26.261333 6.485334c-33.642667 45.056-64.533333 78.912-92.672 101.546666l-5.909333 4.757334-2.837334 0.597333-52.544-14.08 11.114667-20.266667c3.562667-6.485333 7.04-13.013333 10.453333-19.626666 7.04-13.504 17.898667-35.797333 32.597334-66.816a21.290667 21.290667 0 0 0-9.984-28.309334l-3.029334-1.450666a21.333333 21.333333 0 0 0-26.368 6.442666c-33.6 45.013333-64.469333 78.826667-92.608 101.482667l-5.909333 4.757333-2.837333 0.597334-52.138667-13.973334 11.114667-20.266666c3.242667-5.888 6.72-12.416 10.453333-19.626667 6.997333-13.461333 17.962667-35.946667 32.896-67.434667a20.970667 20.970667 0 0 0-10.112-28.010666l-3.328-1.536a21.333333 21.333333 0 0 0-26.069333 6.613333c-33.642667 45.056-64.554667 78.976-92.778667 101.696l-5.909333 4.757333-2.837334 0.597334-32.64-8.746667 11.093334-20.245333c3.541333-6.506667 7.04-13.034667 10.453333-19.626667 6.976-13.482667 17.941333-35.968 32.874667-67.456a21.056 21.056 0 0 0-10.069334-28.074667l-3.242666-1.514666a21.333333 21.333333 0 0 0-26.154667 6.549333z" fill="#333333" p-id="4968"></path></svg>

                                <span style='font-size: 14px;white-space: nowrap;'>清空内容</span>
                            </div>

		                </div>
                    </div>
                </div>`);
                let col = luckysheet_touchmove_startPos.x
                let row = luckysheet_touchmove_startPos.y
                // let col=event.target.getBoundingClientRect().left - $("#mobileOperation").width()
                // let row=event.target.getBoundingClientRect().top
                
                // let col = Store.touchCoord.x+'px'
                // let row = Store.touchCoord.y+'px'
                $("#mobileOperation").css({"left": col, "top": row});   //定位操作区域，这几个变量上面有计算
                // nby 第二步，对具体的按钮进行处理
                // 监听仅复制值的按钮，就是只复制单元格的文本，不包括批注、背景等
                $('#copyShowVal').on('touchend',function(event){

                    let copyData=luckysheet.getRangeValue()
                    // 生成luckysheet的二维数组copyData，因为只复制文本值，所以取单元格对象的m字段
                    // {fa: 'General', t: 'n'}
                    copyData=copyData.map((arrCell)=>arrCell.map(cell=>({v:cell?.m,m:cell?.m,ct:{fa: 'General', t: 'n'}}))) 
                    selection.copybyformat(event,JSON.stringify(copyData))
                    Store.isCopyText = true //判定是点击的仅复制文本还是全复制按钮

                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)//这里可以封装为一个函数,下面经常用。
                })


                let firstRow = luckysheet.getRange()[0]['row'][0]
                // 上插入
                $('#upInsert').on('touchend',function(event){
                    luckysheet.insertRow(firstRow)   
                    
                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)//这里可以封装为一个函数,下面经常用。     
                })
                // 下插入
                $('#downInsert').on('touchend',function(event){
                    luckysheet.insertRowBottom(firstRow)
                    
                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)//这里可以封装为一个函数,下面经常用。
                })
                // 删除行
                $('#delRow').on('touchend',function(event){
                    luckysheet.deleteRow(firstRow,firstRow)
                    
                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)//这里可以封装为一个函数,下面经常用。
                })
                // 清空内容
                $('#delCell').on('touchend',function(event){
                    luckysheet.clearRange()
                    
                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)//这里可以封装为一个函数,下面经常用。
                })
                

                $('#mergeCellMobile').on('touchend',function(event){
              
                    // $("#luckysheet-icon-merge-button").click()
                    // 换成源码
                    // if (!checkProtectionNotEnable(Store.currentSheetIndex)) {
                    //     return;
                    // }
        
                    // if (selectIsOverlap()) {
                    //     if (isEditMode()) {
                    //         alert("不能合并重叠区域");
                    //     }
                    //     else {
                    //         tooltip.info("不能合并重叠区域", "");
                    //     }
                    //     return;
                    // }
        
                    if (Store.config["merge"] != null) {
                        let has_PartMC = false;
        
                        for (let s = 0; s < Store.luckysheet_select_save.length; s++) {
                            let r1 = Store.luckysheet_select_save[s].row[0],
                                r2 = Store.luckysheet_select_save[s].row[1];
                            let c1 = Store.luckysheet_select_save[s].column[0],
                                c2 = Store.luckysheet_select_save[s].column[1];
        
                            has_PartMC = hasPartMC(Store.config, r1, r2, c1, c2);
        
                            if (has_PartMC) {
                                break;
                            }
                        }
        
                        // if (has_PartMC) {
                        //     if (isEditMode()) {
                        //         alert("无法对部分合并单元格执行此操作");
                        //     }
                        //     else {
                        //         tooltip.info("无法对部分合并单元格执行此操作", "");
                        //     }
                        //     return;
                        // }
                    }
                    let d = editor.deepCopyFlowData(Store.flowdata);
                    menuButton.updateFormat_mc(d, "mergeAll");

                    // 换成源码


                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)//这里可以封装为一个函数,下面经常用。
                })

                // 监听粘贴
                $('#mobilePasteBtn').on('touchend',function(event){
                    // nby  以下会对粘贴做一些限制，直接从luckysheet本身的粘贴按钮复制过来
                    if (isEditMode() || Store.allowEdit === false) {//此模式下禁用粘贴
                        return;
                    }
                    if ($(event.target).hasClass("formulaInputFocus")) {
                        return;
                    }
                    if (Store.luckysheet_select_save.length > 1) {
                        if (isEditMode()) {
                            alert(locale_drag.noPaste);
                        }
                        else {
                            tooltip.info(locale_drag.noPaste, "");
                        }
                        return;
                    }
                    selection.isPasteAction = true;
                    selection.paste(event,'btn',Store.isCopyText)   // nby Store.isCopyText全局变量用来
                    luckysheetactiveCell();
                   
                    event.stopPropagation();
                    event.stopImmediatePropagation()

                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)
                })

                //   监听全复制按钮
                $('#mobileCopyBtn').on('touchend',function(event){
                    //复制时存在格式刷状态，取消格式刷
                    if (menuButton.luckysheetPaintModelOn) {
                        menuButton.cancelPaintModel();
                    }

                    if (Store.luckysheet_select_save.length == 0) {
                        return;
                    }

                    //复制范围内包含部分合并单元格，提示
                    if (Store.config["merge"] != null) {
                        let has_PartMC = false;

                        for (let s = 0; s < Store.luckysheet_select_save.length; s++) {
                            let r1 = Store.luckysheet_select_save[s].row[0],
                                r2 = Store.luckysheet_select_save[s].row[1];
                            let c1 = Store.luckysheet_select_save[s].column[0],
                                c2 = Store.luckysheet_select_save[s].column[1];

                            has_PartMC = hasPartMC(Store.config, r1, r2, c1, c2);

                            if (has_PartMC) {
                                break;
                            }
                        }

                        if (has_PartMC) {
                            if (isEditMode()) {
                                alert(locale_drag.noMerge);
                            }
                            else {
                                tooltip.info(locale_drag.noMerge, "");
                            }
                            return;
                        }
                    }

                    //多重选区 有条件格式时 提示
                    let cdformat = Store.luckysheetfile[getSheetIndex(Store.currentSheetIndex)].luckysheet_conditionformat_save;
                    if (Store.luckysheet_select_save.length > 1 && cdformat != null && cdformat.length > 0) {
                        let hasCF = false;

                        let cf_compute = conditionformat.getComputeMap();

                        label:
                        for (let s = 0; s < Store.luckysheet_select_save.length; s++) {
                            if (hasCF) {
                                break;
                            }

                            let r1 = Store.luckysheet_select_save[s].row[0],
                                r2 = Store.luckysheet_select_save[s].row[1];
                            let c1 = Store.luckysheet_select_save[s].column[0],
                                c2 = Store.luckysheet_select_save[s].column[1];

                            for (let r = r1; r <= r2; r++) {
                                for (let c = c1; c <= c2; c++) {
                                    if (conditionformat.checksCF(r, c, cf_compute) != null) {
                                        hasCF = true;
                                        continue label;
                                    }
                                }
                            }
                        }

                        if (hasCF) {
                            if (isEditMode()) {
                                alert(locale_drag.noMulti);
                            }
                            else {
                                tooltip.info(locale_drag.noMulti, "");
                            }
                            return;
                        }
                    }

                    //多重选区 行不一样且列不一样时 提示
                    if (Store.luckysheet_select_save.length > 1) {
                        let isSameRow = true,
                            str_r = Store.luckysheet_select_save[0].row[0],
                            end_r = Store.luckysheet_select_save[0].row[1];
                        let isSameCol = true,
                            str_c = Store.luckysheet_select_save[0].column[0],
                            end_c = Store.luckysheet_select_save[0].column[1];

                        for (let s = 1; s < Store.luckysheet_select_save.length; s++) {
                            if (Store.luckysheet_select_save[s].row[0] != str_r || Store.luckysheet_select_save[s].row[1] != end_r) {
                                isSameRow = false;
                            }
                            if (Store.luckysheet_select_save[s].column[0] != str_c || Store.luckysheet_select_save[s].column[1] != end_c) {
                                isSameCol = false;
                            }
                        }

                        if ((!isSameRow && !isSameCol) || selectIsOverlap()) {
                            if (isEditMode()) {
                                alert(locale_drag.noMulti);
                            }
                            else {
                                tooltip.info(locale_drag.noMulti, "");
                            }
                            return;
                        }
                    }
                    // nby 全复制也需要做数据自适应
                    // selection.copy(event);
                    selection.copybyformat(event,JSON.stringify(luckysheet.getRangeValue()))

                    // Store.isCopyText = false//这是全复制，故需要将复制文本关闭
                    Store.isCopyText = true; //这是全复制，故需要将复制文本关闭
                    Store.luckysheet_paste_iscut = false;
                    luckysheetactiveCell();

                    event.stopPropagation();
                    event.stopImmediatePropagation()
                    
                    setTimeout(()=>{
                        $('#mobileOperation').remove()
                    },200)
                    
                })

                // nby isFormApp 需要判断
                // if(Store.isFormApp){
                //     if(!luckysheet.getRangeValue()[0][0]?.isDetail){
                //         $('#mobileOperation').remove()
                //     }
                    
                // }
            }
        }
        
   
    }
}